package com.ruoyi.ruoyixingyue.modules.api.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.ruoyixingyue.modules.api.entity.File;
import com.ruoyi.ruoyixingyue.modules.api.entity.Media;
import com.ruoyi.ruoyixingyue.modules.api.service.IFileService;
import com.ruoyi.ruoyixingyue.modules.api.service.IMediaService;
import com.ruoyi.ruoyixingyue.modules.utils.oss.UploadFileToOssUtils;
import com.ruoyi.ruoyixingyue.modules.utils.oss.UploadVideoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Author 卢壮志
 * @Date 2024/2/21
 * @Time: 20:15
 */
@Api("上传文件")
@RestController
@Slf4j
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private IFileService fileService;

    @Autowired
    private IMediaService mediaService;

    @PostMapping("/img")
    @ApiOperation("上传图片接口")
    public R teacherListAPI_001(MultipartFile file) {
        String path = UploadFileToOssUtils.uploadFile(file);
        File readyfile = new File();
        readyfile.setFilename(file.getOriginalFilename());
        readyfile.setType(1);
        readyfile.setPlatform(2);
        readyfile.setUrl(path);
        readyfile.setStatus(2);
        fileService.save(readyfile);
        return R.ok(path,"上传成功");
    }

    @PostMapping("/video")
    @ApiOperation("上传视频接口")
    public R teacherListAPI_002(MultipartFile file) {
        UploadVideoDto uploadVideoDto = UploadFileToOssUtils.uploadVideo(file);
        Media media = new Media();
        media.setMediaUrl(uploadVideoDto.getPath());
        media.setDuration(uploadVideoDto.getDuration());
        media.setPlatform(0);
        media.setSize(uploadVideoDto.getSize());
        media.setStatus(2);
        mediaService.save(media);
        uploadVideoDto.setId(media.getId());
        return R.ok(uploadVideoDto,"上传成功");
    }
}
